<template>
	<view>
		<cu-custom :bgColor="NavBarColor" :isBack="true" backFlag="navigate">
			<block slot="backText">返回</block>
			<block slot="content">反馈</block>
		</cu-custom>
		<view class="bg-gray text-gray padding">
			反馈内容
		</view>
		<form>
			<view class="cu-form-group">
				<view class="title">意见</view>
				<input placeholder="请输入意见" name="input" v-model="form.opinion"></input>
				<!-- <textarea placeholder="请输入意见" name="input" type="text" v-model="form.opinion"></textarea> -->
				<!-- <view class="error" v-if="!isOpinionValid">{{ errorMessage }}</view> -->
			</view>
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-blue lg" :disabled="!isFormValid" @click="submit">提交</button>
			</view>
		</form>
		<!-- <form>
			
			<view class="cu-form-group">
				<view class="title">意见</view>
				<textarea placeholder="请输入意见" name="input" type="text" v-model="form.opinion"></textarea> 
			</view>
			
			<view class="cu-form-group">
				<view class="title">附件</view>
				<view class="grid col-4 grid-square flex-sub">
					
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red radius" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			
			
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-blue lg"  @click="submit">提交</button>
			</view>
			
		</form> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				index: -1,
				switchC: true,
				imgList: [],
				uploadUrl: "/sys/common/upload",
				myFormData: {
					avatar: '',
					realname: '',
					username: '',
					sex: 1,
					birthday: '',
					orgCode: '',
					workNo: '',
					phone: '',
					email: '',
					id: '',
				},
				url: {
					toTransfer: '/task_new/taskEnterprise/enterpriseToTransfer',
					toSupervise: '/task_new/taskEnterprise/enterpriseToUrge',
				},
				item: {},
				form: {
					opinion: '',
				},
				errorMessage: '意见是必填项',
			};
		},
		onLoad: function(option) {
			// console.log("this.$Route.query11111111111111",this.$route.params);
			// const params = this.$route.params; 
			this.item = JSON.parse(decodeURIComponent(option.item));
			console.log("传参接收的item", this.item);
		},
		computed: {
			isOpinionValid() {
				return this.form.opinion.trim() !== '';
			},
			isFormValid() {
				return this.isOpinionValid;
			}
		},
		methods: {
			onSubmit() {
				let myForm = this.myFormData
				let checkPhone = new RegExp(/^[1]([3-9])[0-9]{9}$/);
				let checkEmail = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
				console.log("myForm", myForm)
				if (!myForm.phone || myForm.phone.length == 0) {
					this.$tip.alert('请输入手机号');
					return false
				}
				if (!checkPhone.test(myForm.phone)) {
					this.$tip.alert('请输入正确的手机号');
					return false
				}
				if (!checkEmail.test(myForm.email)) {
					this.$tip.alert('请输入正确的邮箱地址');
					return false
				}
				this.myFormData.id = this.$store.getters.userid
				if (this.switchC) {
					this.myFormData.sex = 1
				} else {
					this.myFormData.sex = 2
				}
				console.log('myform', this.myFormData)
				this.$tip.loading();
				this.$http.put('/sys/user/appEdit', this.myFormData).then(res => {
					console.log(res)
					this.$tip.loaded();
					if (res.data.success) {
						this.$tip.toast('提交成功')
						this.$Router.replace({
							name: 'userdetail'
						})
						/* uni.navigateTo({
							url: '/pages/user/userdetail'
						}) */
					}
				}).catch(() => {
					this.$tip.loaded();
					this.$tip.error('提交失败')
				});
			},
			submit() {
				let targetUrl = ''
				//判断sourceName准备接口url
				if (this.item.sourceName === '转办岗') {
					targetUrl = this.url.toTransfer
				}
				if (this.item.sourceName === '督办岗') {
					targetUrl = this.url.toSupervise
				}
				console.log('目标url', targetUrl)
				//准备对象
				//主表对象
				this.item.taskDetailList = []
				this.item.taskDetailList.push(this.form)
				//校验附件和意见是否填写
				//调用反馈接口
				this.$http.post(targetUrl, this.item).then(res => {
					console.log('反馈接口返回值', res)
					if (res.data.success) {
						this.$tip.toast(res.data.message);
						this.$router.go(-2);
					} else {
						this.$tip.toast(res.data.message);
						this.$router.go(-2);
					}
				});

			},
			DateChange(e) {
				this.myFormData.birthday = e.detail.value
			},
			SwitchC(e) {
				this.switchC = e.detail.value
			},
			ChooseImage() {
				var that = this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						that.$http.upload(that.$config.apiUrl + that.uploadUrl, {
								filePath: res.tempFilePaths[0],
								name: 'file'
							})
							.then(res => {
								that.myFormData.avatar = res.data.message;
							})
							.catch(err => {
								that.$tip.error(err.data.message)
							});
						this.imgList = res.tempFilePaths
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '召唤师',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>